import { ref, computed } from "vue";

export default () => {
  const combineArr = ref([]);
  let combineIds = computed(() => {
    return combineArr.value.map((it) => it.id);
  });

  function handleSize() {
    // 处理尺寸
    let xArr = combineArr.value.map((it) => it.x);
    let yArr = combineArr.value.map((it) => it.y);
    let minX = Math.min(...xArr);
    let minY = Math.min(...yArr);

    let maxXArr = combineArr.value.map((it) => it.x + it.width);
    let maxYArr = combineArr.value.map((it) => it.y + it.height);

    let maxX = Math.max(...maxXArr);
    let maxY = Math.max(...maxYArr);
    let width = maxX - minX;
    let height = maxY - minY;
    // const maxXObj = combineArr.value.reduce((prev, current) => {
    //   return prev.x > current.x ? prev : current;
    // });
    // const maxYObj = combineArr.value.reduce((prev, current) => {
    //   return prev.y > current.y ? prev : current;
    // });
    // let width = maxXObj.x + maxXObj.width - minX;
    // let height = maxYObj.y + maxYObj.height - minY;
    return {
      x: minX,
      y: minY,
      width,
      height,
      resizable: false,
      id: GenNonDuplicateID(6),
      props: {
        x: minX,
        y: minY,
        children: combineArr.value,
      },
      active: true,
      componentName: "combineGroup",
    };
  }
  function GenNonDuplicateID(randomLength) {
    return Number(
      Math.random().toString().substr(3, randomLength) + Date.now()
    ).toString(36);
  }
  return {
    combineArr,
    combineIds,
    handleSize,
  };
};
